package work20241216.homework;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author zdl
 * @date 2024/12/16
 */

/**
 * 使用ArrayList方法写
 */
public class DemoKTV {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        List<String> list = new ArrayList<>();
        list.add("奇迹再现");
        list.add("月空中最亮的星");
        list.add("姐就是女王");
        list.add("甜蜜蜜");
        list.add("月半小夜曲");

        System.out.println("-------------欢迎来到点歌系统-------------");
        System.out.println("初始歌曲列表:" + list);
        boolean flag = true;
        while (flag) {
            System.out.println("0.添加歌曲至列表");
            System.out.println("1.将歌曲置顶");
            System.out.println("2.将歌曲前移一位");
            System.out.println("3.退出");
            System.out.print("请输入要执行的操作标号:");
            int num = sc.nextInt();
            switch (num) {
                case 0:
                    System.out.println("请输入要添加的歌曲名称:");
                    String songName = sc.next();
                    add(songName, list);
                    break;
                case 1:
                    System.out.println("请输入要置顶的歌曲名称:");
                    String songName1 = sc.next();
                    top(songName1, list);
                    break;
                case 2:
                    System.out.println("请输入要置前的歌曲名称:");
                    String songName2 = sc.next();
                    prev(songName2, list);
                    break;
                case 3:
                    System.out.println("-------------------退出----------------");
                    flag = false;
                    break;
                default:
                    System.out.println("请输入正确的操作编号(0-3)!!!");
                    flag = false;
            }
        }
    }

    public static void add(String songName, List<String> list) {
        if (list.contains(songName)) {
            System.out.println("歌曲已存在！");
        } else {
            list.add(songName);
            System.out.println("添加后歌曲列表:" + list);
        }
    }
    public static void top(String songName,List<String> list){
        if (list.contains(songName)){
            list.remove(songName);
            list.add(0,songName);
            System.out.println("置顶后歌曲列表:" + list);
        }else {
            System.out.println("您输入的《" + songName + "》歌曲不存在,无法置顶");
        }
    }

    public static void prev(String songName,List<String> list){
        if (list.contains(songName)){
            int index = list.indexOf(songName);
            if (index == 0){
                System.out.println("《"+songName+"》已经是置顶位置了！");
            }else {
                list.remove(songName);
                list.add(index-1,songName);
                System.out.println("前移一位后歌曲列表:" + list);
            }
        }else {
            System.out.println("您输入的《" + songName + "》歌曲不存在,无法前移一位");
        }
    }
}
